package com.qh.pay.dao;

import com.qh.common.config.JsonTypeHandler;
import com.qh.pay.domain.PayQrConfigDO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

/**
 * 聚富扫码通道配置
 * @date 2017-12-14 14:37:38
 */
@Mapper
public interface PayQrConfigDao {

	@Select(
			"select `id`,`out_channel`,`account_no`,`account_name`,`account_phone`,`memo`,`qrs`,cost_rate,create_date,min_money,max_money," +
			"update_date,money,status,use_status,pay_status,provinces,end_date from " +
			"pay_qr_config where id = #{value}")
//	@Results({
//		@Result(column = "qrs", jdbcType = JdbcType.VARCHAR, property = "qrs", typeHandler = JsonTypeHandler.class) })
	PayQrConfigDO get(Integer id);
	
	@Select("select `id`,`out_channel`,`account_no`,`account_name`,`account_phone`,`memo`,`qrs`,cost_rate,create_date,min_money,max_money," +
			"update_date,money,status,use_status,pay_status,provinces,end_date from pay_qr_config "
			+ "where out_channel = #{outChannel} and  account_no = #{accountNo}")
//	@Results({
//			@Result(column = "qrs", jdbcType = JdbcType.VARCHAR, property = "qrs", typeHandler = JsonTypeHandler.class) })
	PayQrConfigDO getByCode(@Param("outChannel") String outChannel, @Param("accountNo") String accountNo);
	
	List<PayQrConfigDO> list(Map<String,Object> map);
	
	int count(Map<String,Object> map);
	
	@Insert("insert into pay_qr_config( `out_channel`,`account_no`,`account_name`,`account_phone`,`memo`,cost_rate,create_date,min_money," +
			"max_money,update_date,use_status,status,pay_status,money,provinces,end_date," +
			"`qrs`)values("
			+ "#{outChannel},#{accountNo},#{accountName},#{accountPhone},#{memo}," +
			"#{costRate},now(),#{minMoney},#{maxMoney},#{updateDate},#{useStatus},#{status},0,#{money}," +
			"#{provinces}," +
			"#{endDate},"
			+ "#{qrs})")
//			+ "#{qrs,typeHandler=com.qh.common.config.JsonTypeHandler})")
	int save(PayQrConfigDO payQrConfig);
	
	int updateByCode(PayQrConfigDO payQrConfig);

	int updateQr(PayQrConfigDO payQrConfig);

	int remove(@Param("id") String id);
	int removeAll();

	@Select("select count(1) from pay_qr_config where   out_channel = #{outChannel} and account_no = #{accountNo}")
	int countByMerchNo(@Param("outChannel") String outChannel, @Param("accountNo") String accountNo);

	/**
	 * @Description 更新二维码图片
	 * @param outChannel

	 * @param qrs
	 * @return
	 */
	@Update("update pay_qr_config set `qrs` = #{qrs,typeHandler=com.qh.common.config.JsonTypeHandler} where  out_channel = #{outChannel} and account_no = #{accountNo}")
	int updateQrs(@Param("outChannel")String outChannel,  @Param("accountNo") String accountNo, @Param("qrs")Map<String, Integer> qrs);


	//更新状态
	@Update("update pay_qr_config set status = #{status},update_date = now() where  account_no = #{accountNo}  and  " +
			"out_channel = #{outChannel}")
	int updateQrState(@Param("status") String status, @Param("accountNo") String accountNo,
					  @Param("outChannel") String outChannel);

	//更新状态
	@Update("update pay_qr_config set use_status = #{useStatus},update_date = now() where  account_no = #{accountNo} " +
			"and out_channel = #{outChannel}")
	int updateQrUseState(@Param("useStatus") String useStatus, @Param("accountNo") String accountNo,
					  @Param("outChannel") String outChannel);


	//更新状态
	@Update("update pay_qr_config set pay_status = #{payStatus},update_date = now(),use_status = 0 where  account_no " +
			"= #{accountNo} " +
			"and out_channel = #{outChannel}")
	int updateQrPayState(@Param("payStatus") String payStatus, @Param("accountNo") String accountNo,
						 @Param("outChannel") String outChannel);
}
